package router

import (
	"github.com/gin-gonic/gin"
	"order/client/client"
	"order/client/request"
	"order/conmon/proto/user"
	"order/pkg"
)

func UserLogin(c *gin.Context) {
	var data request.UserLogin
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
	}
	login, err := client.UserLogin(c, &user.UserLoginReq{
		Username: data.Username,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1001,
			"data": err.Error(),
		})
		return
	}
	claims := pkg.CustomClaims{ID: uint(login.Greet)}
	token, err := pkg.NewJWT("2209a").CreateToken(claims)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 402,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": token,
	})

}
func GoodsList(c *gin.Context) {
	var data request.GoodsList
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	list, err := client.GoodsList(c, &user.GoodsListReq{GoodsStatus: data.GoodsStatus})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1001,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
	})
}
func OrderGoods(c *gin.Context) {
	var data request.OrderGoods
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1000,
			"data": err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	goods, err := client.OrderGoods(c, &user.OrderGoodsReq{
		UserId:  int64(userId),
		GoodsId: data.GoodsId,
		Num:     data.Num,
	})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1001,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": goods,
	})

}
func Alipay(c *gin.Context) {

}
func OrderList(c *gin.Context) {
	userId := c.GetUint("userId")
	list, err := client.OrderList(c, &user.OrderListReq{UserId: int64(userId)})
	if err != nil {
		c.JSON(200, gin.H{
			"code": 1001,
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
	})
}
